﻿using System;
using System.Web;
using Data.Entity;
using Service;
using System.Linq;

namespace ECommerce.Controls
{
    public partial class ProductProperties : BaseUserControl
    {
		private IProductService _ProductService;
		private IShoppingCartItemService _ShoppingCartItemService;
		private IPaymentMethodService _PaymentMethodService;
		private IShippingMethodService _ShippingMethodService;

        public Product TheProductDataSource
        {
			get
			{
				if(CommonHelper.QueryStringInt("productid") > 0)
				{
					if(_ProductService == null)
					{
						_ProductService = new ProductService();
					}

					Product theProduct = _ProductService.GetProduct(CommonHelper.QueryStringInt("productid"));

					return theProduct;
				}
				else
				{
					return null;
				}
			}
		}

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
				BindData();

				BindPaymentMethods();

				BindSupportedShippingMethods();
			}
        }

		protected void Page_PreRender(object sender, EventArgs e)
		{
			this.ButtonAddToCart.Text = GetLocalResourceString("ECommerce.AddToCart");

			this.LProductPriceLabel.Text = GetLocalResourceString("ProductProperty.ProductPriceLabel");
			this.LProductPaymentMethodLabel.Text = GetLocalResourceString("ProductProperty.ProductPaymentMethodLabel");
			this.LProductShippingMethodLabel.Text = GetLocalResourceString("ProductProperty.ProductShippingMethodLabel");
			this.LQuantityLabel.Text = GetLocalResourceString("ProductProperty.QuantityLabel");
			this.LCashTicketLimitLabel.Text = GetLocalResourceString("ProductProperty.CashTicketLimitLabel");
			this.LOperateItem.Text = GetLocalResourceString("ProductProperties.OperateItem");
		}

		protected void ButtonAddToCart_Click(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~/HomePage.aspx");
			}

			var user = SiteContext.Current.User;

			if(user == null)
			{
				Server.Transfer("~/Login.aspx");
			}

			if(_ShoppingCartItemService == null)
			{
				_ShoppingCartItemService = new Service.ShoppingCartItemService();
			}

			ShoppingCartItem theNewCart = new ShoppingCartItem();

			theNewCart.CreatedOn = DateTime.Now;
			theNewCart.ProductID = CommonHelper.QueryStringInt("productid");
			theNewCart.Quantity = 1;
			theNewCart.ShoppingCartTypeID = (int)ShoppingCartType_Const.ShoppingCart;
			theNewCart.UpdatedOn = DateTime.Now;
			theNewCart.UserID = new Guid(user.ProviderUserKey.ToString());

			_ShoppingCartItemService.CreateNewShoppingCartItem(theNewCart);
		}

		private void BindPaymentMethods()
		{
			if(_PaymentMethodService == null)
			{
				_PaymentMethodService = new PaymentMethodService();
			}

			var pm = _PaymentMethodService.GetAllPaymentMethod();

			RepeaterPaymentMethods.DataSource = pm;
			RepeaterPaymentMethods.DataBind();
		}

		private void BindSupportedShippingMethods()
		{
			if(_ShippingMethodService == null)
			{
				_ShippingMethodService = new ShippingMethodService();
			}
			var theProductID = CommonHelper.QueryStringInt("productid");

			if(theProductID > 0)
			{
				var shippingMethods = _ShippingMethodService.GetShippingMethodByProduct(theProductID);

				RepeaterShippingMethods.DataSource = shippingMethods;
				RepeaterShippingMethods.DataBind();
			}
		}

        private void BindData()
        {
            if (TheProductDataSource != null)
            {
                this.LabelProdName.Text = HttpUtility.HtmlEncode(TheProductDataSource.Name);
                this.LabelProdPrice.Text = HttpUtility.HtmlEncode(TheProductDataSource.ListPrice.Value.ToString("c2"));
                this.LabelProdQuantity.Text = HttpUtility.HtmlEncode(TheProductDataSource.Quantity.Value.ToString());
				this.LabelCashTicketLimit.Text = HttpUtility.HtmlEncode(TheProductDataSource.CashTicketLimit.GetValueOrDefault().ToString("c2"));

				this.Page.Title = TheProductDataSource.Name + "-" + GetLocalResourceString("ECommerce.SiteName");
            }
        }
    }
}